//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zaicheng.cloud.remotecall.template;

import com.zaicheng.cloud.remotecall.Util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UriUtils {
    private static final Pattern PCT_ENCODED_PATTERN = Pattern.compile("%[0-9A-Fa-f][0-9A-Fa-f]");

    public UriUtils() {
    }

    public static boolean isEncoded(String value, Charset charset) {
        byte[] var2 = value.getBytes(charset);
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte b = var2[var4];
            if (!isUnreserved((char)b) && b != 37) {
                return false;
            }
        }

        return PCT_ENCODED_PATTERN.matcher(value).find();
    }

    public static String encode(String value) {
        return encodeChunk(value, Util.UTF_8, false);
    }

    public static String encode(String value, Charset charset) {
        return encodeChunk(value, charset, false);
    }

    public static String encode(String value, boolean allowReservedCharacters) {
        return encodeInternal(value, Util.UTF_8, allowReservedCharacters);
    }

    public static String encode(String value, Charset charset, boolean allowReservedCharacters) {
        return encodeInternal(value, charset, allowReservedCharacters);
    }

    public static String decode(String value, Charset charset) {
        try {
            return URLDecoder.decode(value, charset.name());
        } catch (UnsupportedEncodingException var3) {
            return value;
        }
    }

    public static boolean isAbsolute(String uri) {
        return uri != null && !uri.isEmpty() && uri.startsWith("http");
    }

    public static String encodeInternal(String value, Charset charset, boolean allowReservedCharacters) {
        Matcher matcher = PCT_ENCODED_PATTERN.matcher(value);
        if (!matcher.find()) {
            return encodeChunk(value, charset, true);
        } else {
            int length = value.length();
            StringBuilder encoded = new StringBuilder(length + 8);
            int index = 0;

            String tail;
            do {
                tail = value.substring(index, matcher.start());
                encoded.append(encodeChunk(tail, charset, allowReservedCharacters));
                encoded.append(matcher.group());
                index = matcher.end();
            } while(matcher.find());

            tail = value.substring(index, length);
            encoded.append(encodeChunk(tail, charset, allowReservedCharacters));
            return encoded.toString();
        }
    }

    private static String encodeChunk(String value, Charset charset, boolean allowReserved) {
        if (isEncoded(value, charset)) {
            return value;
        } else {
            byte[] data = value.getBytes(charset);

            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                Throwable var5 = null;

                try {
                    byte[] var6 = data;
                    int var7 = data.length;

                    for(int var8 = 0; var8 < var7; ++var8) {
                        byte b = var6[var8];
                        if (isUnreserved((char)b)) {
                            bos.write(b);
                        } else if (isReserved((char)b) && allowReserved) {
                            bos.write(b);
                        } else {
                            pctEncode(b, bos);
                        }
                    }

                    String var21 = new String(bos.toByteArray(), charset);
                    return var21;
                } catch (Throwable var18) {
                    var5 = var18;
                    throw var18;
                } finally {
                    if (bos != null) {
                        if (var5 != null) {
                            try {
                                bos.close();
                            } catch (Throwable var17) {
                                var5.addSuppressed(var17);
                            }
                        } else {
                            bos.close();
                        }
                    }

                }
            } catch (IOException var20) {
                throw new IllegalStateException("Error occurred during encoding of the uri: " + var20.getMessage(), var20);
            }
        }
    }

    private static void pctEncode(byte data, ByteArrayOutputStream bos) {
        bos.write(37);
        char hex1 = Character.toUpperCase(Character.forDigit(data >> 4 & 15, 16));
        char hex2 = Character.toUpperCase(Character.forDigit(data & 15, 16));
        bos.write(hex1);
        bos.write(hex2);
    }

    private static boolean isAlpha(int c) {
        return c >= 97 && c <= 122 || c >= 65 && c <= 90;
    }

    private static boolean isDigit(int c) {
        return c >= 48 && c <= 57;
    }

    private static boolean isGenericDelimiter(int c) {
        return c == 58 || c == 47 || c == 63 || c == 35 || c == 91 || c == 93 || c == 64;
    }

    private static boolean isSubDelimiter(int c) {
        return c == 33 || c == 36 || c == 38 || c == 39 || c == 40 || c == 41 || c == 42 || c == 43 || c == 44 || c == 59 || c == 61;
    }

    private static boolean isUnreserved(int c) {
        return isAlpha(c) || isDigit(c) || c == 45 || c == 46 || c == 95 || c == 126;
    }

    private static boolean isReserved(int c) {
        return isGenericDelimiter(c) || isSubDelimiter(c);
    }

    private boolean isPchar(int c) {
        return isUnreserved(c) || isSubDelimiter(c) || c == 58 || c == 64;
    }
}
